﻿using fuzhu;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using XXX.Models;
using XXX.Models.XXXEntities;

namespace XXX.Bo
{
    public class BarcodeManager
    {
        public string BARCODENO { get; set; }
        public string INVCODE { get; set; }
        public string INVNAME { get; set; }
        public string CSTATE { get; set; }
         public string CPOSCODE { get; set; }  //所属货位
        public int BARLEVEL { get; set; }    //条码级别
        public decimal TOTALQTY { get; set; }
        public List<BarcodeBox> BOXLIST { get; set; }
        public List<BarcodeContent> LIST { get; set; }


        //生成新条码规则
        public string GenNewBarcode()
        {
            int iMax = 0;
            string sql = string.Format(@"SELECT MAX(RIGHT(BARCODENO,6)) FROM dbo.WMS_BARCODE WHERE BARCODENO LIKE 'A%'
AND LEN(BARCODENO)= 7");
            Object obejct = DbHelper2.ExecuteScalar(sql, WMSService.conStr);
            if (obejct != null)
            {
                iMax = DbHelper.GetDbInt(obejct);
            }
            return "A" + (iMax + 1).ToString("000000");

        }

        //根据明细保存
        public string SaveFromList()
        {
            SqlTransaction tran = DbHelper2.BeginTrans(WMSService.conStr);
            try
            {
                
                //先存储明细
                for (int i = 0; i < LIST.Count; i++)
                {
                    SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@BARCODENO",LIST[i].BARCODENO),
                         new SqlParameter("@QTY",LIST[i].QTY),
                         new SqlParameter("@INVCODE",LIST[i].INVCODE),
                         new SqlParameter("@BATCH",DbHelper.ToDbValue(LIST[i].BATCH)),
                          new SqlParameter("@FREE1",DbHelper.ToDbValue(LIST[i].FREE1)),
                         new SqlParameter("@FREE2",DbHelper.ToDbValue(LIST[i].FREE2)),
                new SqlParameter("@VENCODE",DbHelper.ToDbValue(LIST[i].VENCODE)),
                new SqlParameter("@MAKEDATE",DbHelper.ToDbValue(LIST[i].MAKEDATE)),
                new SqlParameter("@SHROWID",DbHelper.ToDbValue(LIST[i].SHROWID))
                };

                  String   sql = string.Format(@"INSERT INTO [dbo].[WMS_BARCODES]
           ([BARCODENO]
           ,[QTY]
           ,[INVCODE]
           ,[BATCH]
           ,[FREE1]
           ,[FREE2]
           ,[VENCODE]
           ,[MAKEDATE]
           ,[SHROWID])values(@BARCODENO
           ,@QTY
           ,@INVCODE
           ,@BATCH
           ,@FREE1
           ,@FREE2
           ,@VENCODE
           ,@MAKEDATE
           ,@SHROWID)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param, tran);
                }
                //插入主表
                SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@BARCODENO", BARCODENO),
                         new SqlParameter("@TOTALQTY",TOTALQTY),
                         new SqlParameter("@INVCODE",INVCODE),
                         new SqlParameter("@CSTATE","未入库"),
                          new SqlParameter("@CPOSCODE",DbHelper.ToDbValue(CPOSCODE)),
                         new SqlParameter("@BARLEVEL",DbHelper.ToDbValue(BARLEVEL)) 
                };

                string sql2 = string.Format(@"INSERT INTO  [WMS_BARCODE]
           ([BARCODENO]
           ,[TOTALQTY]
           ,[INVCODE]
           ,[CSTATE]
           ,[DDATE]
           ,[CTYPE]
           ,[CPOSCODE]
           ,[BARLEVEL])values(@BARCODENO
           ,@TOTALQTY
           ,@INVCODE
           ,@CSTATE
           ,GETDATE()
           ,NULL
           ,@CPOSCODE
           ,@BARLEVEL)");
                DbHelper2.ExecuteSqlWithTrans(sql2, param2, tran);

                //存储汇总
                DbHelper2.CommitTransAndCloseConnection(tran);
                return "ok";
            }
            catch (Exception ex)
            {
                DbHelper2.RollbackAndCloseConnection(tran);
                return ex.Message;
            }
}

        //根据明细保存，没有箱码
        public string UpdateFromListWithNoBox()
        {
            SqlTransaction tran = DbHelper2.BeginTrans(WMSService.conStr);
            try
            {

                //先存储明细
                for (int i = 0; i < LIST.Count; i++)
                {
                    SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@BARCODENO",LIST[i].BARCODENO),
                         new SqlParameter("@QTY",LIST[i].QTY),
                         new SqlParameter("@INVCODE",LIST[i].INVCODE),
                         new SqlParameter("@BATCH",DbHelper.ToDbValue(LIST[i].BATCH)),
                          new SqlParameter("@FREE1",DbHelper.ToDbValue(LIST[i].FREE1)),
                         new SqlParameter("@FREE2",DbHelper.ToDbValue(LIST[i].FREE2)),
                new SqlParameter("@VENCODE",DbHelper.ToDbValue(LIST[i].VENCODE)),
                new SqlParameter("@MAKEDATE",DbHelper.ToDbValue(LIST[i].MAKEDATE)),
                new SqlParameter("@SHROWID",DbHelper.ToDbValue(LIST[i].SHROWID))
                };

                    String sql = string.Format(@"INSERT INTO [dbo].[WMS_BARCODES]
           ([BARCODENO]
           ,[QTY]
           ,[INVCODE]
           ,[BATCH]
           ,[FREE1]
           ,[FREE2]
           ,[VENCODE]
           ,[MAKEDATE]
           ,[SHROWID])values(@BARCODENO
           ,@QTY
           ,@INVCODE
           ,@BATCH
           ,@FREE1
           ,@FREE2
           ,@VENCODE
           ,@MAKEDATE
           ,@SHROWID)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param, tran);
                }

                //并箱后数量
                string sql2 = string.Format(@"update  dbo.WMS_BARCODE set totalqty = {1}
 WHERE  barcodeno='{0}'", BARCODENO, TOTALQTY);
                DbHelper2.ExecuteSqlWithTrans(sql2, tran);

                

                //存储汇总
                DbHelper2.CommitTransAndCloseConnection(tran);
                return "ok";
            }
            catch (Exception ex)
            {
                DbHelper2.RollbackAndCloseConnection(tran);
                return ex.Message;
            }
        }


        /// <summary>
        /// 查询条码
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel GetBarcode(string ccode, string clx)
        {
            try
            {
                RetModel retModel = new RetModel();
                SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@cbarcode",ccode),
                 new SqlParameter("@clx",clx),
                new SqlParameter("@result",SqlDbType.NVarChar,200)

          };
                param[2].Direction = ParameterDirection.Output;

                DataTable dt = DbHelper2.Execute("sp_checkbarcode", param, CommandType.StoredProcedure, WMSService.conStr).Tables[0];
                //检查条码是否存在
                string cRe = DbHelper.GetDbString(param[2].Value);
                if (cRe == "ok")
                {
                    List<BarcodeContent> u8wms001s1 = TableListConverter<BarcodeContent>.TableToList2(dt);
                    List<object> u8wms002s = u8wms001s1.ConvertAll(s => (object)s);
                    return DBService.ReOK(u8wms002s);
                }
                else
                {
                    return DBService.ReError(cRe);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReError(ex.Message);
            }

        }

        /// <summary>
        /// 查询条码，带下级内容
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel GetBarcodeBox(string ccode, string clx)
        {
            try
            {
                RetModel retModel = new RetModel();
                string sql = string.Format(@"SELECT BARCODENO,a.INVCODE,TOTALQTY,CSTATE,b.INVNAME,a.CPOSCODE FROM dbo.WMS_BARCODE a
LEFT JOIN dbo.AA_INVENTORY b  ON a.INVCODE = b.INVCODE WHERE BARCODENO = '{0}' ", ccode);


                DataTable dt = DbHelper2.Execute(sql,WMSService.conStr).Tables[0];
                if (dt.Rows.Count == 0)
                {
                    throw new Exception("条码不正确");
                }
                string cStatus = DbHelper.GetDbString(dt.Rows[0]["CSTATE"]);
                if (cStatus != "在库")
                {
                    throw new Exception(string.Format("条码状态为{0},不可操作", cStatus));
                }
                BarcodeManager barcode = new BarcodeManager();

                barcode.BARCODENO = ccode;
                barcode.INVCODE = DbHelper.GetDbString(dt.Rows[0]["INVCODE"]);
                barcode.INVNAME = DbHelper.GetDbString(dt.Rows[0]["INVNAME"]);
                barcode.TOTALQTY = DbHelper.GetDbdecimal(dt.Rows[0]["TOTALQTY"]);
                barcode.CSTATE = DbHelper.GetDbString(dt.Rows[0]["CSTATE"]);
                barcode.CPOSCODE = DbHelper.GetDbString(dt.Rows[0]["CPOSCODE"]);
                //检查装箱的条码
                sql = String.Format(@" SELECT  CHILDNO,b.INVCODE,b.TOTALQTY FROM WMS_BARCODE_BOX a
    LEFT  JOIN  dbo.WMS_BARCODE b ON a.CHILDNO = b.BARCODENO WHERE A.BARCODENO='{0}'", ccode);
                DataTable dt2 = DbHelper2.ExecuteTable(sql, WMSService.conStr);
                if (dt2.Rows.Count > 0)
                {
                    List<BarcodeBox> barcodeBox = TableListConverter<BarcodeBox>.TableToList2(dt2);
                    barcode.BOXLIST = barcodeBox;
                }


                List<BarcodeManager> u8wms001s1 = new List<BarcodeManager>();
                u8wms001s1.Add(barcode);
                List<object> u8wms002s = u8wms001s1.ConvertAll(s => (object)s);
                return DBService.ReOK(u8wms002s);


            }
            catch (Exception ex)
            {
                return DBService.ReError(ex.Message);
            }

        }




        /// <summary>
        /// 查询当前货位上的条码
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel GetPositionInvBar(string ccode)
        {
            try
            {
               

                string sql = string.Format(@"SELECT a.CPOSCODE,a.BARCODENO, b.INVCODE,b.INVNAME,a.TOTALQTY,a.CSTATE FROM dbo.WMS_BARCODE a
LEFT JOIN dbo.AA_INVENTORY b ON a.INVCODE = b.invcode
WHERE CSTATE = '在库' and  a.CPOSCODE='{0}'", ccode);
                DataTable dt = DbHelper2.Execute(sql, WMSService.conStr).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    List<PositionInvBarcode> u8wms001s1 = TableListConverter<PositionInvBarcode>.TableToList2(dt);
                    List<object> u8wms002s = u8wms001s1.ConvertAll(s => (object)s);
                    return DBService.ReOK(u8wms002s);
                }
                else
                {
                    return DBService.ReError("当前货位无条码");
                }

            }
            catch (Exception ex)
            {
                return DBService.ReError(ex.Message);
            }

        }


        /// <summary>
        /// 上架
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModelbool PositionUp(PostionChange postionChange)
        {
            try
            {


                for (int i = 0; i < postionChange.barcodelist.Count; i++)
                {

                    string sql = string.Format(@"update  dbo.WMS_BARCODE set CPOSCODE='{0}'
 WHERE  barcodeno='{1}'", postionChange.POSCODE, postionChange.barcodelist[i].BARCODENO);
                    DbHelper2.ExecuteNonQuery(sql, WMSService.conStr);

                }
                return DBService.ReOKbool();
            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

        /// <summary>
        /// 下架
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModelbool PositionDown(PostionChange postionChange)
        {
            try
            {


                for (int i = 0; i < postionChange.barcodelist.Count; i++)
                {

                    string sql = string.Format(@"update  dbo.WMS_BARCODE set CPOSCODE=null
 WHERE  barcodeno='{0}'", postionChange.barcodelist[i].BARCODENO);
                    DbHelper2.ExecuteNonQuery(sql, WMSService.conStr);

                }
                return DBService.ReOKbool();
            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }


        /// <summary>
        /// 拆箱
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModelbool ChaiXiang(ScanBarcodeChaiXiang model)
        {
            try
            {
                //拆箱类型区分
                if (model.CTYPE == "最小包装拆箱")
                {
                    //检查条码是否是混批的，混批的无法通过最小包装拆箱     
                    string sql = string.Format(@"select a.INVCODE,BATCH,QTY,FREE1,FREE2,VENCODE,MAKEDATE,SHROWID,b.CPOSCODE,b.BARLEVEL from wms_barcodes a,
 wms_barcode b WHERE  a.barcodeno = b.barcodeno and a.barcodeno='{0}'", model.BARCODENO);
                    DataTable DT = DbHelper2.ExecuteTable(sql, WMSService.conStr);
                    if (DT.Rows.Count > 1)
                    {
                        throw new Exception("当前条码为混批，不能使用最小包装拆箱");
                    }
                    else
                    {
                        //先创建条码

                        if (model.SMALLQTY == "0")
                        {
                            throw new Exception(" 最小包装数不能为0");
                        }
                        //可拆分的箱数，往上取整
                        decimal dSumQty = DbHelper.GetDbdecimal(DT.Rows[0]["QTY"]);
                        int iCnt = (int)Math.Ceiling(dSumQty / DbHelper.GetDbdecimal(model.SMALLQTY));

                        BarcodeManager barcode = new BarcodeManager();
                        barcode.BARLEVEL = DbHelper.GetDbInt(DT.Rows[0]["BARLEVEL"]) + 1;
                        barcode.CPOSCODE = DbHelper.GetDbString(DT.Rows[0]["CPOSCODE"]);
                        barcode.INVCODE = DbHelper.GetDbString(DT.Rows[0]["INVCODE"]);
                       

                        List<BarcodeBox> lstbox = new List<BarcodeBox>();
                        decimal qty = DbHelper.GetDbdecimal(model.SMALLQTY);
                        for (int i = 0; i < iCnt; i++)
                        {
                            //每行是个新的条码
                            List<BarcodeContent> lst = new List<BarcodeContent>();
                            BarcodeContent barcode1 = new BarcodeContent();
                            BarcodeBox barcodeBox = new BarcodeBox();
                            barcode1.INVCODE = barcode.INVCODE;
                            barcode1.BATCH = DbHelper.GetDbString(DT.Rows[0]["BATCH"]);
                            barcode1.FREE1 = DbHelper.GetDbString(DT.Rows[0]["FREE1"]);
                            barcode1.FREE2 = DbHelper.GetDbString(DT.Rows[0]["FREE2"]);
                            barcode1.VENCODE = DbHelper.GetDbString(DT.Rows[0]["VENCODE"]);
                            barcode1.MAKEDATE = DbHelper.GetDbDate(DT.Rows[0]["MAKEDATE"]);
                            barcode1.SHROWID = DbHelper.GetDbInt(DT.Rows[0]["SHROWID"]);
                        

                            barcode1.BARCODENO = barcode.GenNewBarcode();
                            barcode.BARCODENO = barcode1.BARCODENO;
                            barcodeBox.CHILDNO= barcode1.BARCODENO;
                            barcodeBox.INVCODE= barcode.INVCODE;
                           
                           
                            if (i == iCnt - 1)
                            {
                                //取最后一箱的数量
                                qty = dSumQty - i * DbHelper.GetDbdecimal(model.SMALLQTY);

                            }
                            barcode1.QTY = qty;
                            barcode.TOTALQTY = qty;
                            barcodeBox.TOTALQTY = qty;
                           //生成条码
                           lst.Add(barcode1);
                            //箱码
                            lstbox.Add(barcodeBox);
                            barcode.LIST = lst;
                            string cRe = barcode.SaveFromList();
                            if (cRe != "ok")
                            {
                                throw new Exception(cRe);
                            }
                        }
                        model.barcodelist = lstbox;
                        //构建model.barcodelist

                    }
                }
                //保存操作记录
                SqlTransaction tran = DbHelper2.BeginTrans(WMSService.conStr);
                try
                {
                    string cParentid = System.Guid.NewGuid().ToString();


                    SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@id",cParentid),
                         new SqlParameter("@OPETRTYPE",model.CTYPE),
                         new SqlParameter("@CUSERID",model.CUSERID),
                         new SqlParameter("@CUSERNAME",model.CUSERNAME),
                          new SqlParameter("@PARENTNO",model.BARCODENO),
                         new SqlParameter("@SMALLQTY",model.SMALLQTY),

          };

                    string sql = string.Format(@"INSERT INTO[dbo].[WMS_BOXOPERATION]
                    ([ID]
          ,[VOUCHTYPE]
          ,[OPETRTYPE]
          ,[CUSERID]
          ,[CUSERNAME]
          ,[DMAKETIME]
          ,[PARENTNO]
          ,[SMALLQTY]) values(@id
          ,'拆箱'
          ,@OPETRTYPE
          ,@CUSERID
          ,@CUSERNAME
          ,getdate()
          ,@PARENTNO
          ,@SMALLQTY)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param2, tran);

                    //更改拆箱明细
                    sql = string.Format(@"update  dbo.WMS_BARCODE set CSTATE='已拆箱',cposcode=null
 WHERE  barcodeno='{0}'", model.BARCODENO);
                    DbHelper2.ExecuteSqlWithTrans(sql, tran);

                    for (int i = 0; i < model.barcodelist.Count; i++)
                    {
                        SqlParameter[] param3 = new SqlParameter[]{
                        new SqlParameter("@PARENTID",cParentid),
                         new SqlParameter("@CHILDNO",model.barcodelist[i].CHILDNO),
                         new SqlParameter("@INVCODE",model.barcodelist[i].INVCODE),
                            new SqlParameter("@QTY",model.barcodelist[i].TOTALQTY )

          };
                        sql = @"INSERT INTO [dbo].[WMS_BOXOPERATIONS]
           ([ID]
           ,[CHILDNO]
           ,[INVCODE]
           ,[QTY]
           ,[PARENTID]) values(newid(),@CHILDNO
           ,@INVCODE
           ,@QTY
           ,@PARENTID
           )";
                        DbHelper2.ExecuteSqlWithTrans(sql, param3, tran);
                        //更改拆箱明细
                        sql = string.Format(@"update  dbo.WMS_BARCODE set CSTATE='在库',CPOSCODE='{1}'
 WHERE  barcodeno='{0}'", model.barcodelist[i].CHILDNO, model.CPOSCODE);
                        DbHelper2.ExecuteSqlWithTrans(sql, tran);
                    }
                    sql = string.Format(@"delete from WMS_BARCODE_box where BARCODENO ='{0}'", model.BARCODENO);
                    DbHelper2.ExecuteSqlWithTrans(sql, tran);



                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }



                return DBService.ReOKbool();
            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }


        /// <summary>
        /// 并箱
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModelbool BingXiang(ScanBarcodeBingXiang model)
        {
            try
            {
                //拆箱类型区分
               
                    //取汇总的产品内容
                    string cFilter = "(";
                    cFilter += "'" + model.BARCODENO  + "'";
                    for (int i = 0; i < model.barcodelist.Count; i++)
                    {
                        cFilter +=",'" + model.barcodelist[i].BARCODE  + "'";
                    }
                    cFilter += ")";

                    string sql = string.Format(@"select a.INVCODE,BATCH,sum(QTY) qty,FREE1,FREE2,VENCODE,MAKEDATE,SHROWID  from wms_barcodes a,
 wms_barcode b WHERE  a.barcodeno = b.barcodeno and a.barcodeno in {0}   group by a.INVCODE,BATCH,FREE1,FREE2,VENCODE,MAKEDATE,SHROWID ", cFilter);
                    DataTable DTbing = DbHelper2.ExecuteTable(sql, WMSService.conStr);
                //写表头，表体，生成条码 
                TOTALQTY = 0.00m;
                BARCODENO = model.BARCODENO;
                LIST = new List<BarcodeContent>();
                if (model.CTYPE == "无子件并箱")
                {
                 
                    

                    for (int i = 0; i < DTbing.Rows.Count; i++)
                    {
                        //每行是个新的条码
                        BarcodeContent barcode1 = new BarcodeContent();
                        barcode1.INVCODE = DbHelper.GetDbString(DTbing.Rows[i]["INVCODE"]);
                        barcode1.BATCH = DbHelper.GetDbString(DTbing.Rows[i]["BATCH"]);
                        barcode1.FREE1 = DbHelper.GetDbString(DTbing.Rows[i]["FREE1"]);
                        barcode1.FREE2 = DbHelper.GetDbString(DTbing.Rows[i]["FREE2"]);
                        barcode1.VENCODE = DbHelper.GetDbString(DTbing.Rows[i]["VENCODE"]);
                        barcode1.MAKEDATE = DbHelper.GetDbDate(DTbing.Rows[i]["MAKEDATE"]);
                        barcode1.SHROWID = DbHelper.GetDbInt(DTbing.Rows[i]["SHROWID"]);
                        barcode1.BARCODENO = model.BARCODENO;
                        barcode1.QTY = DbHelper.GetDbdecimal(DTbing.Rows[i]["qty"]); ;
                        TOTALQTY += barcode1.QTY;

                        LIST.Add(barcode1);
                    } 
                            string cRe = UpdateFromListWithNoBox();
                            if (cRe != "ok")
                            {
                                throw new Exception(cRe);
                            }
                       
                         
                }
                //保存操作记录
                SqlTransaction tran = DbHelper2.BeginTrans(WMSService.conStr);
                try
                {
                    string cParentid = System.Guid.NewGuid().ToString();


                    SqlParameter[] param2 = new SqlParameter[]{
                        new SqlParameter("@id",cParentid),
                         new SqlParameter("@OPETRTYPE",model.CTYPE),
                         new SqlParameter("@CUSERID",model.CUSERID),
                         new SqlParameter("@CUSERNAME",model.CUSERNAME),
                          new SqlParameter("@PARENTNO",model.BARCODENO),

          };

                    sql = string.Format(@"INSERT INTO[dbo].[WMS_BOXOPERATION]
                    ([ID]
          ,[VOUCHTYPE]
          ,[OPETRTYPE]
          ,[CUSERID]
          ,[CUSERNAME]
          ,[DMAKETIME]
          ,[PARENTNO]) values(@id
          ,'并箱'
          ,@OPETRTYPE
          ,@CUSERID
          ,@CUSERNAME
          ,getdate()
          ,@PARENTNO)");
                    DbHelper2.ExecuteSqlWithTrans(sql, param2, tran);

                   
                    for (int i = 0; i < model.barcodelist.Count; i++)
                    {
                        SqlParameter[] param3 = new SqlParameter[]{
                        new SqlParameter("@PARENTID",cParentid),
                         new SqlParameter("@CHILDNO",model.barcodelist[i].BARCODE),
                         new SqlParameter("@INVCODE",model.barcodelist[i].INVCODE),
                            new SqlParameter("@QTY",model.barcodelist[i].QUANTITY )

          };
                        sql = @"INSERT INTO [dbo].[WMS_BOXOPERATIONS]
           ([ID]
           ,[CHILDNO]
           ,[INVCODE]
           ,[QTY]
           ,[PARENTID]) values(newid(),@CHILDNO
           ,@INVCODE
           ,@QTY
           ,@PARENTID
           )";
                        DbHelper2.ExecuteSqlWithTrans(sql, param3, tran);
                        //并箱明细
                        sql = string.Format(@"update  dbo.WMS_BARCODE set CSTATE='已并箱',CPOSCODE='{1}'
 WHERE  barcodeno='{0}'", model.barcodelist[i].BARCODE, model.CPOSCODE);
                        DbHelper2.ExecuteSqlWithTrans(sql, tran);
                    }
                    //sql = string.Format(@"delete from WMS_BARCODE_box where BARCODENO ='{0}'", model.BARCODENO);
                    //DbHelper2.ExecuteSqlWithTrans(sql, tran);



                    DbHelper2.CommitTransAndCloseConnection(tran);
                }
                catch (Exception ex)
                {
                    DbHelper2.RollbackAndCloseConnection(tran);
                    throw new Exception(ex.Message);
                }



                return DBService.ReOKbool();
            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }

        }

        //更改条码的状态
        public void ChangeBarcodestate(string parentid, string cstate)
        {
            //更细条码的状态
            string sql = string.Format(@"UPDATE a SET cstate='{1}' FROM dbo.WMS_BARCODE a,dbo.WMS_RDRECORDS_ORI b WHERE a.BARCODENO=b.BARCODENO
AND b.PARENTID = '{0}' ", parentid, cstate);
            DbHelper2.ExecuteNonQuery(sql, WMSService.conStr);
        }
    }


    /// <summary>
    /// 条码内容
    /// </summary>
    public class BarcodeContent
    {
        public string BARCODENO { get; set; }
        public string INVCODE { get; set; }
        public string CSTATE { get; set; }
        public string BATCH { get; set; }
        public decimal QTY { get; set; }
        public decimal TOTALQTY { get; set; }
        public string FREE1 { get; set; }
        public string FREE2 { get; set; }
        public string VENCODE { get; set; }
        public int SHROWID { get; set; }    //新增送货单id
        public int BARLEVEL { get; set; }    //新增条码层级
        public DateTime MAKEDATE { get; set; }
    }

    /// <summary>
    /// 条码内容
    /// </summary>
    public class BarcodeBox
    {
        public string CHILDNO { get; set; }
        public string INVCODE { get; set; }
        public decimal TOTALQTY { get; set; }
    }


    /// <summary>
    ///拆箱-扫码保存内容
    /// </summary>
    public class ScanBarcodeChaiXiang
    {
        public string BARCODENO { get; set; }
        public string INVCODE { get; set; }
        public string INVNAME { get; set; }
        public string CSTATE { get; set; }
        public decimal TOTALQTY { get; set; }
        public string CTYPE { get; set; }
        public string CUSERID { get; set; }
        public string CUSERNAME { get; set; }
        public string CPOSCODE { get; set; }
        public string SMALLQTY { get; set; }
        public DateTime DDATE { get; set; }
        public List<BarcodeBox> barcodelist { get; set; }
    }

    /// <summary>
    ///拆箱-扫码保存内容
    /// </summary>
    public class ScanBarcodeBingXiang
    {
        public string BARCODENO { get; set; }
        public string INVCODE { get; set; }
        public string INVNAME { get; set; }
        public string CSTATE { get; set; }
        public decimal TOTALQTY { get; set; }
        public string CTYPE { get; set; }
        public string CUSERID { get; set; }
        public string CUSERNAME { get; set; }
        public string CPOSCODE { get; set; }
        public string SMALLQTY { get; set; }
        public DateTime DDATE { get; set; }
        public List<BingXiangs> barcodelist { get; set; }
    }

    public class BingXiangs
    {
        public string BARCODE { get; set; }
        public Decimal QUANTITY { get; set; }
        public string INVCODE { get; set; }
        public int XH { get; set; }
    }
    }
